#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ================================================================
#
#
#    Filename: formatPy.py
#      Author: Andy
#     Created: 2023-06-24 09:08:26
# Description:
#
# ================================================================
import autopep8
import os
import sys

def format_python_code_in_folder(folder_path):
    # Recursive function to search for Python files
    def search_python_files(path):
        python_files = []
        for root, _, filenames in os.walk(path):
            for filename in filenames:
                if filename.endswith('.py'):
                    python_files.append(os.path.join(root, filename))
        return python_files

    # Get all Python files in the folder
    python_files = search_python_files(folder_path)

    # Format each Python file using autopep8
    for file_path in python_files:
        # Read the contents of the file
        with open(file_path, 'r') as file:
            code = file.read()

        # Format the code using autopep8
        formatted_code = autopep8.fix_code(code)

        # Write the formatted code back to the file
        with open(file_path, 'w') as file:
            file.write(formatted_code)

if __name__ == '__main__':
    folder_path = '.'
    if len(sys.argv) > 1:
        folder_path = sys.argv[1]

    format_python_code_in_folder(folder_path)

